﻿using Newtonsoft.Json;
using NicoView.Model.Service.Files;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WindowsCommonStorage;

namespace NicoView.Model.Service.Errors
{
    public static class ErrorManager
    {
        private const string ERROR_FOLDERNAME = "Errors";

        private const string ERROR_FILENAME = "Error";




        public static async Task SaveExceptionAsync<T>(T e)
            where T:Exception
            
        {
            try
            {
                
                var exceptionDataFolder = await CommonAppllicationData.Current.LocalFolder.CreateFolderAsync(ERROR_FOLDERNAME, CommonCreationCollisionOption.OpenIfExists).ConfigureAwait(false);

                var exceptonDataFile = await exceptionDataFolder.CreateFileAsync(ERROR_FILENAME + "_" + Guid.NewGuid() + "_" + DateTime.Now.ToString("yyyy年MM月dd日HH時mm分ss秒fffミリ秒") + ".log", CommonCreationCollisionOption.GenerateUniqueName).ConfigureAwait(false);

                using (var writer = new StreamWriter(await exceptonDataFile.OpenStreamAsync().ConfigureAwait(false)))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(writer, e);

                }

            }
            catch (Exception ex) { }


        }
    }
}
